package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TCashCard;
import com.ruipengkj.bean.TSendCardRecord;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TCashCardService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberCashCardDetail extends BaseDialog{
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancelButton;//取消
	private JLabelJTextFieldComponent number = null;//会员编号
	private JLabelJTextFieldComponent catalogID = null;///会员卡号
	private JLabelJTextFieldComponent nameTF = null; //姓名
	private JLabelJComboBoxComponent userType = null; //使用类型
	private JLabelJComboBoxComponent pulishType = null; //发放类型
	private DataPackageComponent happenTimeTF = null;//创建时间
	private DataPackageComponent startTimeTF = null;//开始时间
	private DataPackageComponent endTimeTF = null;//结束时间
	//private JLabelJTextFieldComponent sexTF = null; //性别
	private JLabelJTextFieldComponent needPoint = null; //积分
	private JLabelJTextFieldComponent initMoneyTF = null;//初始金额
	private TCashCardService tCashCardService=BuildSpringApplication.getInstance().getTCashCardServiceImpl();
	
	public boolean isSuccess=false;//判断是否添加成功
	public MemberCashCardDetail(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		String name=nameTF.getText().getText().trim();//会员姓名
		//int sex=Integer.valueOf(sexTF.getText().getText().trim());//会员姓名
		String cardNum=catalogID.getText().getText().trim();//
		String needPoints=needPoint.getText().getText().trim(); //手机号码
		String initMoney=initMoneyTF.getText().getText().trim();//初始金额
		
		Date date=new Date();
		TSendCardRecord tSendCardRecord=new TSendCardRecord();
		tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
		tSendCardRecord.setCardNum(cardNum);
		tSendCardRecord.setSendCardTime(date);
		TCashCard tCashCard=new TCashCard();
		tCashCard.setCardID(number.getText().getText().trim());
		tCashCard.setCreator(name);
		//tUser.setSex(sex);
		tCashCard.setMoney(Double.parseDouble(initMoney));
		tCashCard.setNeedPoint(Integer.parseInt(needPoints));
		tCashCard.setEndTime(endTimeTF.getDate());
		tCashCard.setStartTime(startTimeTF.getDate());
		tCashCard.setHappenTime(happenTimeTF.getDate());
		tCashCard.setCatalogID(cardNum);
		tCashCard.setUseType(userType.getBox().getSelectedIndex()+"");
		tCashCard.setPublishType(pulishType.getBox().getSelectedIndex()+"");
		if(e.getSource()==okButton){
			//确定
			try {
				if(needPoints.equals("")||!MatcherFormatUtil.MatcherNumberScheme(needPoints)){
					JOptionPane.showMessageDialog(null, "赠送积分 不能为空或格式不正确。");
					needPoint.getText().setText("");
					needPoint.getText().requestFocus();
					return;
				}
				if(userType.getBox().getSelectedIndex() == 0){
					JOptionPane.showMessageDialog(null, "请选择使用类型");
					return;
				}
				if(pulishType.getBox().getSelectedIndex() == 0){
					JOptionPane.showMessageDialog(null, "请选择发放类型");
					return;
				}
				if(name.equals("")||name.length()>20){
					JOptionPane.showMessageDialog(null, "会员名称不能为空，且不能大于20个字");
					nameTF.getText().requestFocus();
					return;
				}
				if(initMoney.equals("")||!MatcherFormatUtil.MatcherPriceScheme(initMoney)){
					JOptionPane.showMessageDialog(null, "消费次数不能为空");
					initMoneyTF.getText().requestFocus();
					initMoneyTF.getText().setText("");
					return;
				}
				tCashCardService.saveOrUpdateService(tCashCard);
				this.isSuccess=true;
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			//BuildSpringApplication.getInstance().getTOpLogServiceImpl().modifyService("");
			this.dispose();
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		number=new JLabelJTextFieldComponent("会员编号：",30);
		catalogID = new JLabelJTextFieldComponent("会员卡号：", 30);
		nameTF=new JLabelJTextFieldComponent("用 户 名：",30);
		userType=new JLabelJComboBoxComponent("使用类型：",new String[]{},188);
		userType.addElement(new JComboBoxPOJO("请选择……"));
		userType.addElement(new JComboBoxPOJO("一次性优惠卷"));
		pulishType=new JLabelJComboBoxComponent("发放类型：",new String[]{},188);
		pulishType.addElement(new JComboBoxPOJO("请选择……"));
//		try {
//			List<TCashCard> tcashCardList= tCashCardService.findService(null);
//			for(int i = 0 ;i<tcashCardList.size();i++){
//				pulishType.addElement(new JComboBoxPOJO("用户领取"));
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		pulishType.addElement(new JComboBoxPOJO("用户领取"));
		pulishType.addElement(new JComboBoxPOJO("店铺发送"));
		needPoint=new JLabelJTextFieldComponent("积分：",30);
		initMoneyTF=new JLabelJTextFieldComponent("消费次数：",30);
		happenTimeTF=new DataPackageComponent("创建时间：",30);
		startTimeTF=new DataPackageComponent("开始时间：",30);
		endTimeTF=new DataPackageComponent("结束时间：",30);
		number.getText().setEditable(false);
		catalogID.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("修改");
		cancelButton = new RPJButtonGray("关闭");
		jp1.add(okButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		
		main_jp.add(number);
		main_jp.add(catalogID);
		main_jp.add(nameTF);
		main_jp.add(userType);
		main_jp.add(pulishType);
		main_jp.add(needPoint);
		main_jp.add(initMoneyTF);
		main_jp.add(happenTimeTF);
		main_jp.add(startTimeTF);
		main_jp.add(endTimeTF);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(TCashCard tcashCard){
		this.isSuccess=false;
		if(tcashCard!=null){
			number.getText().setText(tcashCard.getCardID());
			catalogID.getText().setText(tcashCard.getCatalogID());
			nameTF.getText().setText(tcashCard.getCreator());
			userType.getBox().setSelectedIndex(Integer.parseInt(tcashCard.getUseType()));
			pulishType.getBox().setSelectedIndex(Integer.parseInt(tcashCard.getPublishType()));
		//	pulishType.getText().setText(tcashCard.getPublishType());
			needPoint.getText().setText(tcashCard.getNeedPoint()+"");
			initMoneyTF.getText().setText(tcashCard.getMoney()+"");
			happenTimeTF.setDate(tcashCard.getHappenTime());
			startTimeTF.setDate(tcashCard.getStartTime());
			endTimeTF.setDate(tcashCard.getEndTime());
		}
		this.setVisible(true);
	}
}
